-- Tab
vim.opt.tabstop = 2                  -- number of visual spaces per TAB
vim.opt.softtabstop = 2              -- number of spacesin tab when editing
vim.opt.shiftwidth = 2               -- insert 2 spaces on a tab
vim.opt.expandtab = true             -- tabs are spaces, mainly because of python
vim.opt.autoindent = true            -- automatically indent
vim.opt.cinkeys = {'0{', '0}', '0)', '0]', '0#', '!^F', 'o', 'O', 'e'}

-- UI config
vim.opt.number = true                -- show absolute number
vim.opt.relativenumber = true        -- show relative number
vim.opt.cursorline = true            -- highlight cursor line underneath the cursor horizontally
vim.opt.guicursor = 'n-v-r-cr:block,i-c-ci-ve:ver25,r-cr:hor20,o:hor50,a:blinkwait700-blinkoff400-blinkon400-Cursor/lCursor,sm:block-blinkwait175-blinkoff150-blinkon175'

-- Searching
vim.opt.ignorecase = true            -- ignore case in searches by default
vim.opt.smartcase = true             -- smart case in searches by default

-- Command mode
vim.opt.wildmenu = true

-- Mouse
vim.opt.mouse = ""

-- clipboard
vim.opt.clipboard:append("unnamedplus")

-- character
vim.opt.list = true
vim.opt.listchars = {tab='  ', trail='▫'} -- □

-- scroll
vim.opt.scrolloff = 3

-- fold
vim.opt.foldcolumn = '0'
vim.opt.foldmethod = "expr"
vim.o.fillchars = [[eob: ,fold: ,foldopen:,foldsep: ,foldclose:]]
vim.opt.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.opt.foldenable = false
vim.opt.foldlevel = 100
-- vim.opt.foldlevelstart = 99
local function fold_virt_text(result, start_text, lnum)
  local text = ''
  local hl
  for i = 1, #start_text do
    local char = start_text:sub(i, i)
    local captured_highlights = vim.treesitter.get_captures_at_pos(0, lnum, i - 1)
    local outmost_highlight = captured_highlights[#captured_highlights]
    if outmost_highlight then
      local new_hl = '@' .. outmost_highlight.capture
      if new_hl ~= hl then
        -- as soon as new hl appears, push substring with current hl to table
        table.insert(result, { text, hl })
        text = ''
        hl = nil
      end
      text = text .. char
      hl = new_hl
    else
      text = text .. char
    end
  end
  table.insert(result, { text, hl })
end
function _G.custom_foldtext()
  local start_text = vim.fn.getline(vim.v.foldstart):gsub('\t', string.rep(' ', vim.o.tabstop))
  local nline = vim.v.foldend - vim.v.foldstart
  local result = {}
  fold_virt_text(result, start_text, vim.v.foldstart - 1)
  -- table.insert(result, { '    ', nil })
  -- table.insert(result, { '◖', '@comment.warning.gitcommit' })
  -- table.insert(result, { ' ... ↙ ' .. nline .. ' lines', '@comment.warning' })
  table.insert(result, { ' ... ↙ ' .. nline .. ' lines', 'DapBreakpointCondition' })
  -- table.insert(result, { '◗', '@comment.warning.gitcommit' })
  return result
end
vim.opt.foldtext = 'v:lua.custom_foldtext()'
